﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using LinqToBing.Bing.Wrapper.Interfaces;

namespace LinqToBing.Bing.Linq
{
    public class LiveQueryProvider : IQueryProvider
    {
        public IQueryable CreateQuery(Expression expression)
        {
            Type elementType = TypeSystem.GetElementType(expression.Type);
            try
            {
                return (IQueryable)Activator.CreateInstance(typeof(QueryableLiveSearchResultBase<>).MakeGenericType(elementType), new object[] { this, expression });
            }
            catch (System.Reflection.TargetInvocationException tie)
            {
                throw tie.InnerException;
            }
        }

        // BMK Restore this...
        
        //public object Execute(Expression expression)
        //{
        //    return LiveSearchContext.Execute(expression, false);
        //}

        // Queryable's "single value" standard query operators call this method.
        // It is also called from QueryableTerraServerData.GetEnumerator().
        //public TResult Execute<TResult>(Expression expression)
        //{
        //    bool IsEnumerable = (typeof(TResult).Name == "IEnumerable`1");

        //    return (TResult)LiveSearchContext.Execute(expression, IsEnumerable);
        //}

        // BMK Restore this...
        
        //// Queryable's collection-returning standard query operators call this method.
        //public IQueryable<TResult> CreateQuery<TResult>(Expression expression)
        //{
        //    //return new QueryableLiveSearchResultBase<TResult>(this, expression);
        //    return (IQueryable)Activator.CreateInstance(typeof(QueryableLiveSearchResultBase<TResult>).MakeGenericType(elementType), new object[] { this, expression });
        //}

        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            throw new NotImplementedException();
        }


        public TResult Execute<TResult>(Expression expression)
        {
            throw new NotImplementedException();
        }

        public object Execute(Expression expression)
        {
            throw new NotImplementedException();
        }
    }
}
